package com.octant.testipc;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.tuya.smart.android.camera.sdk.TuyaIPCSdk;
import com.tuya.smart.android.camera.sdk.api.ITuyaIPCCore;
import com.tuya.smart.camera.camerasdk.typlayer.callback.AbsP2pCameraListener;
import com.tuya.smart.camera.camerasdk.typlayer.callback.OperationDelegateCallBack;
import com.tuya.smart.camera.ipccamerasdk.bean.MonthDays;
import com.tuya.smart.camera.middleware.p2p.ITuyaSmartCameraP2P;
import com.tuya.smart.camera.middleware.widget.AbsVideoViewCallback;
import com.tuya.smart.camera.middleware.widget.TuyaCameraView;
import com.tuya.smart.home.sdk.TuyaHomeSdk;
import com.tuya.smart.sdk.api.ITuyaDevice;
import com.tuya.smart.sdk.bean.DeviceBean;

import java.nio.ByteBuffer;

import androidx.appcompat.app.AppCompatActivity;

public class DeviceLiveVideoActivity extends AppCompatActivity {

    private Context mContext;
    private TuyaCameraView mVideoView;
    private String devId;
    private ITuyaSmartCameraP2P mCameraP2P;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_live_video);
        mContext = DeviceLiveVideoActivity.this;
        devId = getIntent().getStringExtra("devId");
        initViews();
    }

    private void initViews() {
        ITuyaIPCCore cameraInstance = TuyaIPCSdk.getCameraInstance();
        if (cameraInstance != null) {
            if (cameraInstance.isIPCDevice(devId) == false) {
                Toast.makeText(mContext, "该设备不是IPC设备，无法显示摄像头内容", Toast.LENGTH_SHORT).show();
            }else {
                mCameraP2P = cameraInstance.createCameraP2P(devId);
            }
        }
        mVideoView = findViewById(R.id.camera_video_view);
        mVideoView.setViewCallback(new AbsVideoViewCallback() {
            @Override
            public void onCreated(Object view) {
                super.onCreated(view);
                //渲染视图构造完成时回调
                mCameraP2P.generateCameraView(mVideoView.createdView());
            }
        });
        mVideoView.createVideoView(devId);
    }

    @Override
    protected void onResume() {
        super.onResume();
        ITuyaIPCCore cameraInstance = TuyaIPCSdk.getCameraInstance();
        if (mCameraP2P != null) {
            mCameraP2P.registerP2PCameraListener(new AbsP2pCameraListener() {
                @Override
                public void onReceiveFrameYUVData(int sessionId, ByteBuffer y, ByteBuffer u, ByteBuffer v, int width, int height, int nFrameRate, int nIsKeyFrame, long timestamp, long nProgress, long nDuration, Object camera) {
                    super.onReceiveFrameYUVData(sessionId, y, u, v, width, height, nFrameRate, nIsKeyFrame, timestamp, nProgress, nDuration, camera);
                }
            });
            mCameraP2P.connect(devId, new OperationDelegateCallBack() {
                @Override
                public void onSuccess(int sessionId, int requestId, String data) {
                    //连接成功
                    mCameraP2P.startPreview(new OperationDelegateCallBack() {
                        @Override
                        public void onSuccess(int sessionId, int requestId, String data) {

                        }

                        @Override
                        public void onFailure(int sessionId, int requestId, int errCode) {
                            Toast.makeText(mContext, "启动摄像头预览失败,"+errCode, Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                @Override
                public void onFailure(int sessionId, int requestId, int errCode) {
                    //连接失败
                    Toast.makeText(mContext, "连接P2P通道失败,"+errCode, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (null != mCameraP2P) {
            mCameraP2P.stopPreview(new OperationDelegateCallBack() {
                @Override
                public void onSuccess(int sessionId, int requestId, String data) {

                }

                @Override
                public void onFailure(int sessionId, int requestId, int errCode) {

                }
            });
            mCameraP2P.disconnect(new OperationDelegateCallBack() {
                @Override
                public void onSuccess(int sessionId, int requestId, String data) {

                }

                @Override
                public void onFailure(int sessionId, int requestId, int errCode) {

                }
            });
            mCameraP2P.removeOnP2PCameraListener();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mCameraP2P) {
            mCameraP2P.destroyP2P();
        }
    }
}
